/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spärrsonatfx;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.*;
import javax.swing.ListSelectionModel;

/**
 *
 * @author leofidjeland
 */
public class SoundMachine {

    //private int[] notes = new int[]{60, 61, 62, 61, 67, 53, 45, 33, 78, 43, 69, 67, 5, 64, 62, 60};
    //private int baseNote = 60; // this is middle C 
    private int[] notes = new int[]{50, 52, 54, 57, 59, 62, 64, 64, 69, 71}; //2 octaves pentatonic scale
    private Synthesizer synthesizer;
    private MidiChannel channel;
    private final Random randomGenerator = new Random();

    public SoundMachine() {
        try {
            synthesizer = MidiSystem.getSynthesizer();
            synthesizer.open();
            channel = synthesizer.getChannels()[0];
            channel.programChange(112);
        } catch (MidiUnavailableException ex) {
            Logger.getLogger(SoundMachine.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    void play(int quantity) {
        while (quantity > 0) {
            int randInt = randomGenerator.nextInt(notes.length);
            int note = notes[randInt];

            channel.noteOn(note, 50);
            quantity--;
        }

    }
}